#include "device_sign_file.h"
#include "device_system_constants.h"
#include <QDir>
#include <QList>
#include <QStorageInfo>
#include <QtConcurrent>
#include <QCryptographicHash>

DeviceSignFile::DeviceSignFile()
{
    JsonHelper::ReadJsonFromFile(DEVICE_FILE_STORAGE_PATH, power_on_device_message_);
}

DeviceSignFile::~DeviceSignFile()
{

}

unsigned char DeviceSignFile::getDeviceNumber() const
{
    return static_cast<unsigned char>(power_on_device_message_[CAMERA_ID].toInt());
}

QString DeviceSignFile::getDeviceSerialNumber() const
{
    QByteArray key = DEVICE_SERIAL_PREFIX;
    QString camera_id = power_on_device_message_[CAMERA_ID].toString();
    key.append(camera_id);
    if(power_on_device_message_[SERIAL_NUMBER] == QString::fromUtf8(QCryptographicHash::hash(key, QCryptographicHash::Sha256))){
        return camera_id;
    }
    return "";
}

int DeviceSignFile::setExposureAmount(double value_update)
{
    QJsonObject obj;
    JsonHelper::ReadJsonFromFile(DEVICE_FILE_STORAGE_PATH, obj);
    JsonHelper::ModifyJson(DEVICE_FILE_STORAGE_PATH, EXPOSURE_AMOUNT, value_update);

    return DEVICE_OK;
}

unsigned int DeviceSignFile::getExposureAmount() const
{
    QJsonObject obj;
    JsonHelper::ReadJsonFromFile(DEVICE_FILE_STORAGE_PATH, obj);
    return static_cast<unsigned int>(obj[EXPOSURE_AMOUNT].toDouble());
}

int DeviceSignFile::clearStorage(unsigned char position, unsigned short number)
{
    if(CLEARALL == number){
        return clearStorageAllData(position);
    }else{
        return clearStoragePartData(position, number);
    }
}

int DeviceSignFile::interStorageRollingOverlay()
{
    QList<QStorageInfo> storageInfoList = QStorageInfo::mountedVolumes();
    foreach (QStorageInfo storage, storageInfoList) {
        if(storage.rootPath().contains(INTERNAL_STORAGE_PATH)){
            if(surplus_storage_ > storage.bytesAvailable()/1048576){
                QtConcurrent::run(this,&DeviceSignFile::deleteEarliestImage);
            }
            return DEVICE_OK;
        }
        return DEVICE_ERR;
    }
    return DEVICE_OK;
}

int DeviceSignFile::getStorageCapacity(unsigned int &internal_storage, unsigned int &external_storage)
{
    QList<QStorageInfo> storageInfoList = QStorageInfo::mountedVolumes();
    foreach (QStorageInfo storage, storageInfoList) {
        if(storage.rootPath().contains(INTERNAL_STORAGE_PATH)){
            internal_storage = static_cast<unsigned int>(storage.bytesAvailable()/1048576);
        }
        if(storage.rootPath().contains(EXTERNAL_STORAGE_PATH)){
            external_storage = static_cast<unsigned int>(storage.bytesAvailable()/1048576);
        }
    }

    return DEVICE_OK;
}

int DeviceSignFile::clearStorageAllData(unsigned char storage_position)
{
    QDir dir;
    if(InternalStorage == storage_position){
        dir.setPath(INTERNAL_STORAGE_PATH);
        if(dir.exists()){
            dir.setSorting(QDir::Name);
            QStringList list = dir.entryList();
            for(auto d : list){
                dir.remove(d);
            }
            return ClearInternalOK;
        }else{
            return ClearErr;
        }
    }else if(ExternalStorage == storage_position){
        dir.setPath(EXTERNAL_STORAGE_PATH);
        if(dir.exists()){
            dir.setSorting(QDir::Name);
            QStringList list = dir.entryList();
            for(auto d : list){
                dir.remove(d);
            }
            return ClearExternalOK;
        }else{
            return ClearErr;
        }
    }else{
        return DEVICE_OK;
    }
}

int DeviceSignFile::clearStoragePartData(unsigned char storage_position, unsigned short number)
{
    QDir dir;
    if(InternalStorage == storage_position){
        dir.setPath(INTERNAL_STORAGE_PATH);
        if(dir.exists()){
            dir.setSorting(QDir::Name);
            QStringList list = dir.entryList();
            if(number > list.size()){
                return ClearErr;
            }
            for(int i = 0; i < number; ++i){
                dir.remove(list.at(i));
            }
            return ClearInternalOK;
        }else{
            return ClearErr;
        }
    }else if(ExternalStorage == storage_position){
        dir.setPath(EXTERNAL_STORAGE_PATH);
        if(dir.exists()){
            dir.setSorting(QDir::Name);
            QStringList list = dir.entryList();
            if(number > list.size()){
                return ClearErr;
            }
            for(int i = 0; i < number; ++i){
                dir.remove(list.at(i));
            }
            return ClearExternalOK;
        }else{
            return ClearErr;
        }
    }else{
        return DEVICE_OK;
    }
}

int DeviceSignFile::deleteEarliestImage()
{
    QDir path(INTERNAL_STORAGE_PATH);
    path.setFilter(QDir::Files);//设置过滤器过滤类型
    path.setSorting(QDir::Name);//根据时间排序，末尾为最早时间
    QStringList string_list = path.entryList();
    for(int i = 0; i < 100; ++i){
        path.remove(string_list.at(i));//删除文件
    }

    return DEVICE_OK;
}
